package com.barneyx.mshinfoplatform.exception.handle;

import com.barneyx.mshinfoplatform.common.ExResult;
import com.barneyx.mshinfoplatform.exception.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@ResponseBody
@Order(100)
@Slf4j
public class GobalExceptonHandle {

    @Order(50)
    @ExceptionHandler(SmsException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public ExResult<?> handleSmsException(SmsException ex) {
        log.error(ex.getMessage());
        ex.printStackTrace();
        return ExResult.ofFail(ex.getCode());
    }

    @Order(60)
    @ExceptionHandler(UserException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public ExResult<?> handleInfoUserException(UserException ex) {
        log.error(ex.getMessage());
        ex.printStackTrace();
        return ExResult.ofFail(ex.getCode());
    }

    @Order(70)
    @ExceptionHandler(InfoException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public ExResult<?> handleInfoException(InfoException ex) {
        log.error(ex.getMessage());
        ex.printStackTrace();
        if(ex.getCode()!=null){
            return ExResult.ofFail(ex.getCode());
        }else{
            return ExResult.ofFail(ex.getMsg());
        }
    }
    @Order(70)
    @ExceptionHandler(StoreException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public ExResult<?> handleInfoException(StoreException ex) {
        log.error(ex.getMessage());
        ex.printStackTrace();
        if(ex.getCode()!=null){
            return ExResult.ofFail(ex.getCode());
        }else{
            return ExResult.ofFail(ex.getMsg());
        }
    }


    @Order(200)
    @ExceptionHandler(MshException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public ExResult<?> handleInfoException(MshException ex) {
        log.error(ex.getMessage());
        ex.printStackTrace();
        if(ex.getCode()!=null){
            return ExResult.ofFail(ex.getCode());
        }else{
            return ExResult.ofFail(ex.getMsg());
        }
    }
    @Order(101)
    @ExceptionHandler(DataIntegrityViolationException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public ExResult<?> handleSQLDataException(DataIntegrityViolationException ex) {
        log.error(ex.getMessage());
        ex.printStackTrace();
        return ExResult.ofFail("操作失败!未知异常,请重新尝试!!!");
    }


    @Order(100)
    @ExceptionHandler(Exception.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public ExResult<?> handleInfoException(Exception ex) {
        log.error(ex.getMessage());
        ex.printStackTrace();
        return ExResult.ofFail(ex.getMessage());
    }

}
